package storage

import (
	"distributed/db"
	"distributed/message"
	"fmt"
	"log"
	"strconv"
)

type Store struct{}

func (s *Store) HackRecent(req message.ReptileResult, resp *message.ReptileResp) error {
	record := db.Record{
		Url:    req.URL,
		Method: req.Method,
		Result: req.Result,
	}
	if err := db.DB.Create(&record).Error; err != nil {
		resp.Status = 500
		resp.Result = err.Error()
		return nil
	}
	if req.ParserResult != nil {
		//fmt.Println("[ ParserResult ]", req.ParserResult)
		for _, v := range *req.ParserResult {
			if err := db.DB.Create(&db.BriefInformationHackPai{
				RecordID:     record.ID,
				DataID:       v.DataID,
				URL:          v.URL,
				Title:        v.Title,
				Introduction: v.Introduction,
			}).Error; err != nil {
				log.Printf("[ RecordStore ] create v:%#v\terror: %v\n", v.DataID, err)
			}
		}
	}

	resp.Status = 200
	resp.Result = "success"
	return nil
}

func (s *Store) Net163HomeHead(req message.ReptileResult, resp *message.ReptileResp) error {
	record := db.Record{
		Url:    req.URL,
		Method: req.Method,
		Result: req.Result,
	}
	if err := db.DB.Create(&record).Error; err != nil {
		resp.Status = 500
		resp.Result = err.Error()
		return nil
	}
	resp.Status = 200
	resp.Result = strconv.Itoa(int(record.ID))
	return nil
}

func (s *Store) Net163WarParser(req message.ReptileResult, resp *message.ReptileResp) error {
	if req.ParserResult == nil {
		resp.Status = 500
		resp.Result = "error: ParserResult is nil"
		return nil
	}

	go func(r *[]message.BriefInformation) {
		for _, v := range *r {
			db.DB.Create(&db.BriefInformationNet163News{
				URL:          v.URL,
				Title:        v.Title,
				Introduction: v.Title,
			})
		}
	}(req.ParserResult)
	resp.Status = 200
	resp.Result = "success"
	return nil
}

func (s *Store) HotPageParser(req message.ReptileResult, resp *message.ReptileResp) error {
	if req.ParserResult != nil {
		for _, v := range *req.ParserResult {
			db.DB.Create(&db.PlayList{
				Title:        v.Title,
				URL:          v.URL,
				Introduction: v.Introduction,
			})
		}
	}
	resp.Status = 200
	resp.Result = "success"
	return nil
}

func (s *Store) SongDetailParser(req message.ReptileResult, resp *message.ReptileResp) error {
	if req.Song != nil {
		song := req.Song
		var image string
		if len(song.Images) != 0 {
			image = song.Images[0]
		}
		db.DB.Create(&db.Song{
			SongID:      song.SongID,
			Title:       song.Title,
			Image:       image,
			AppID:       song.AppID,
			Description: song.Description,
			PubDate:     song.PubDate,
		})
	}
	resp.Status = 200
	resp.Result = "success"
	return nil
}

func (s *Store) CommentApiParser(req message.ReptileResult, resp *message.ReptileResp) error {
	if req.ParserResult != nil {
		url := req.URL
		fmt.Printf("[ CommentApiParser ] url: %v\tlimit: %v\toffset: %v\n", url, req.Limit, req.Offset)
		//comments := make([]Comment, commentLen)
		var comments []db.Comment
		for _, v := range *req.ParserResult {
			if v.Comment == nil {
				fmt.Printf("[ CommentApiParser error ] Comment is nil %v\n", url)
				continue
			}
			comment := db.Comment{
				SongID:    url,
				CommentID: v.Comment.CommentID,
				Content:   v.Comment.Content,
				Time:      v.Comment.Time,
				UserID:    v.Comment.User.UserID,
				Nickname:  v.Comment.User.Nickname,
				AvatarURL: v.Comment.User.AvatarURL,
			}
			if db.DB.Create(&comment).Error == nil {
				comments = append(comments, comment)
				//fmt.Printf("[ CommentApiParser comment ]  %#v\n", comment)
			}
		}
		var _chan chan []db.Comment
		if len(CommentChan) != 0 {
			_chan = <-CommentChan
			_chan <- comments
		}
	}

	resp.Status = 200
	resp.Result = "success"
	return nil
}
